<template>
  <div class="favorite">
    <van-nav-bar
      title="我的收藏"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <div v-if="coffeeLikeAllData.length > 0">
      <Bg>
        <div class="content-box">
          <div
            class="product-box"
            v-for="item in coffeeLikeAllData"
            :key="item.id"
          >
            <div class="product-img">
              <img :src="item.smallImg" class="auto-img" alt="" />
            </div>
            <div class="name">{{ item.name }}</div>
            <div class="enner">{{ item.enname }}</div>
            <div class="price">
              ￥{{ item.price
              }}<van-icon name="delete-o" @click="deleLike(item.pid)" />
            </div>
          </div>
        </div>
      </Bg>
    </div>
    <div v-else>
      <van-empty description="没有该状态的订单" />
    </div>
  </div>
</template>

<script>
import "../assets/less/favorite.less";
import Bg from "../components/Bg.vue";
export default {
  name: "Favorite",
  components: {
    Bg,
  },
  data() {
    return {
      coffeeLikeAllData: [],
    };
  },
  created() {
    this.coffeeLikeAll();
  },
  methods: {
    // 查询用户收藏信息
    coffeeLikeAll() {
      let tokenString = this.$cookies.get("tokenString");

      if (!tokenString) {
        return this.$router.push({ name: "Login" });
      }

      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0,
      });

      this.axios({
        method: "get",
        url: this.baseUrl + "/findAllLike",
        params: {
          appkey: this.appkey,
          tokenString: tokenString,
        },
      })
        .then((result) => {
          console.log(result);
          this.$toast.clear();
          if (result.data.code == 700) {
            return this.$router.push({ name: "Login" });
          }
          if (result.data.code === 2000) {
            this.coffeeLikeAllData = result.data.result;
            console.log(this.coffeeLikeAllData);
          } else {
            this.$toast({
              message: result.data.msg,
              forbidClick: true,
              duration: 3000,
            });
          }
        })
        .catch((err) => {
          this.$toast.clear();
          console.log(err);
        });
    },

    // 取消收藏
    deleLike(pid) {
      let tokenString = this.$cookies.get("tokenString");

      //当用户未登录的时候跳转到登录页面
      if (!tokenString) {
        return this.$router.push({ name: "Login" });
      }

      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0,
      });

      this.axios({
        method: "post",
        url: this.baseUrl + "/notlike",
        data: {
          appkey: this.appkey,
          pid: pid,
          tokenString: tokenString,
        },
      })
        .then((result) => {
          console.log(result);
          this.$toast.clear();
          if (result.data.code === 700) {
            this.$toast({
              message: result.data.msg,
              forbidClick: true,
              duration: 3000,
            });
            return this.$router.push({ name: "Login" });
          }
          if (result.data.code === 900) {
            if (result.data.result === 1) {
              this.coffeeLikeAll();
            }
          } else {
            this.$toast({
              message: result.data.msg,
              forbidClick: true,
              duration: 3000,
            });
          }
        })
        .catch((err) => {
          this.$toast.clear();
          console.log(err);
        });
    },

    // 返回
    onClickLeft() {
      this.$router.go(-1);
    },
  },
};
</script>
